package com.vnp.vchat.version2;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.vnp.vchat.R;
import com.vnp.vchat.adapter.ContactAdapter;
import com.vnp.vchat.bean.ObjContact;
import com.vnp.vchat.database.MySQLiteHelper;
import com.vnp.vchat.util.Utils;

public class InvitePhoneActivity extends Activity implements OnClickListener {

	ListView listContact;
	ContactAdapter adapter;
	ArrayList<ObjContact> objContacts = new ArrayList<ObjContact>();
	MySQLiteHelper db;
	TextView title;

	ImageButton btnBack, btndone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_invite_phone);
		SharedPreferences prefs = getPreferences(0);
		db = new MySQLiteHelper(this);

		objContacts = db.getContactFromDB();

		for (int i = 0; i < objContacts.size(); i++) {
			Log.i("PHONE", "NAME: " + objContacts.get(i).getName());
		}
		btndone = (ImageButton) findViewById(R.id.btn_done);
		title = (TextView) findViewById(R.id.title);
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnBack.setVisibility(View.VISIBLE);

		listContact = (ListView) findViewById(R.id.list_contact);
		listContact.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
			ObjContact oj=	(ObjContact) parent.getAdapter().getItem(position);
			oj.setCheck();
			adapter.notifyDataSetChanged();
				

			}
		});
		// adapter = new ContactAdapter(InvitePhoneActivity.this, objContacts);
		adapter = new ContactAdapter(InvitePhoneActivity.this, objContacts);
		listContact.setAdapter(adapter);

		init();

		setFont();
	}

	void init() {
		title.setText("Số điện thoại");
		btnBack.setOnClickListener(this);
		btndone.setOnClickListener(this);
	}

	void setFont() {
		title.setTypeface(Utils.getTypefaceBold(InvitePhoneActivity.this));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_function1:
			closeActivity();
			break;
		case R.id.btn_back:
			onBackPressed();
			break;
		case R.id.btn_done:
			sendSMS(adapter.getSelectedGroup());
			break;
		default:

			break;
		}
	}

	private void sendSMS(ArrayList<ObjContact> arr) {
		String newphone="";
		for (int i = 0; i < arr.size(); i++) {
			ObjContact objphone=arr.get(i);
			String phone=objphone.getPhone();
			if(phone.length()>7)
			newphone=newphone+";"+phone;
		}
		
		newphone= newphone.substring(1);
		
		
		Intent smsIntent = new Intent(Intent.ACTION_VIEW);
		smsIntent.setData(Uri.parse("smsto:"+newphone));
		smsIntent.putExtra("sms_body", getString(R.string.title_sms));
		try {
			 startActivity(smsIntent);
			finish();
		} catch (android.content.ActivityNotFoundException ex) {
		}

	}

	@Override
	public void onBackPressed() {
		closeActivity();
	}

	void closeActivity() {
		finish();
		overridePendingTransition(R.anim.over_main_right,
				R.anim.trans_right_out);
	}

}
